//
//  ConverFileViewViewController.m
//  VideoPlayer
//
//  Created by Tema on 2019/3/8.
//  Copyright © 2019 tema.tian. All rights reserved.
//

#import "DTConverFileViewViewController.h"
#import "DTFFmpegManager.h"

@interface DTConverFileViewViewController ()

@property (weak, nonatomic) IBOutlet UITextView *inputTextView;
@property (weak, nonatomic) IBOutlet UITextView *outputTextView;
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

@end

@implementation DTConverFileViewViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  if (self.inputPath && self.outputPath) {
    self.inputTextView.text = self.inputPath;
    self.outputTextView.text = self.outputPath;
    [self startButton:nil];
  } else {
    // 测试数据
    NSString *inputPath = [[NSBundle mainBundle] pathForResource:@"video.mp4" ofType:nil];
    self.inputTextView.text = inputPath;
    
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *outputPath = [docDir stringByAppendingPathComponent:@"videoConver.mov"];
    self.outputTextView.text = outputPath;
  }
}

- (IBAction)startButton:(id)sender {
  if (!self.inputTextView.text.length || !self.outputTextView.text.length) {
    self.progressLabel.text = @"请先输入路径";
    return ;
  }
  
  [[DTFFmpegManager sharedManager] converWithInputPath:self.inputTextView.text
                                          outputPath:self.outputTextView.text
                                        processBlock:^(float process) {
                                          self.progressLabel.text = [NSString stringWithFormat:@"转码中 %.2f%%", process * 100];
                                          self.progressView.progress = process;
                                        }
                                     completionBlock:^(NSError *error) {
                                       if (error) {
                                         NSLog(@"转码失败 : %@", error);
                                         self.progressLabel.text = @"转码失败";
                                       } else {
                                         NSLog(@"转码成功，请在相应路径查看，默认在沙盒Documents路径");
                                         self.progressLabel.text = @"恭喜，转码成功！";
                                       }
                                     }];
  
}

@end

